package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    private final StringRedisTemplate stringRedisTemplate;
    private final IUserService userService;

    /**
     * 查询当前用户是否有关注这个人
     *
     * @param followUserId
     * @return
     */
    @Override
    public Result isFollow(Long followUserId) {
        Long userId = UserHolder.getUser().getId();
        if (userId == null) {
            return Result.ok();
        }
        Integer count = query().eq("follow_user_id", followUserId).eq("user_id", userId).count();
        return Result.ok(count > 0);
    }

    /**
     * 点击关注按钮时 关注和取消关注
     *
     * @param followUserId
     * @param isFollow
     * @return
     */
    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        // 获取当前登陆用户的id
        Long userId = UserHolder.getUser().getId();
        if (userId == null) {
            return Result.ok();
        }
        // 判断是否关注
        String key = "follow:" + userId;

        if (isFollow) {
            // 如果点了关注就新增数据
            Follow follow = new Follow().setFollowUserId(followUserId).setUserId(userId);
            boolean isSuccess = save(follow);
            if (isSuccess) {
                stringRedisTemplate.opsForSet().add(key, followUserId.toString());
            }
        } else {
            // 已经关注再点关注就删除数据
            boolean isSuccess = remove(new QueryWrapper<Follow>().eq("follow_user_id", followUserId).eq("user_id", userId));
            if (isSuccess) {
                stringRedisTemplate.opsForSet().remove(key, followUserId.toString());
            }
        }
        return Result.ok();
    }

    @Override
    public Result comonFollow(Long followUserId) {
        // 求目标用户和当前用户的交集

        // 获取当前用户
        Long userID = UserHolder.getUser().getId();
        String user_key = "follow:" + userID;
        String follow_key = "follow:" + followUserId;

        // 获取id集合
        Set<String> CommonFollow = stringRedisTemplate.opsForSet().union(user_key, follow_key);

        // 解析id集合
        if (CommonFollow == null || CommonFollow.size() == 0) {
            return Result.ok(Collections.emptyList());
        }

        // 查询用户
        // 转换类型
        List<UserDTO> userDTOS = userService.listByIds(CommonFollow)
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());

        return Result.ok(userDTOS);
    }
}
